package cn.wujiangbo.test;

import cn.wujiangbo.test.thread.MyIgnorePolicy;
import cn.wujiangbo.test.thread.NameTreadFactory;

import java.util.concurrent.*;

/**
 线程池（自定义线程池、自定义线程工厂、自定义拒绝策略）
 */
public class ThreadTest_5 {

    public static void main(String[] args) throws Exception {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2,
                4,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(2),
                new NameTreadFactory(),
                new MyIgnorePolicy());

        for (int i = 1; i <= 10; i++) {
            MyTask3 task = new MyTask3(i);
            executor.execute(task);
        }
    }
}

class MyTask3 implements Runnable {

    private Integer name;

    public MyTask3(Integer name) {
        this.name = name;
    }

    @Override
    public void run() {
        try {
            System.out.println(name + "：Thread=" + Thread.currentThread().getName() + " is running!");
            Thread.sleep(3000); //让任务执行慢点
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return "MyTask [name=" + name + "]";
    }
}